//app.js
var cfg = require('utils/cfg');

App({
  onLaunch: function () {
    let that = this;
    // 登录
    that.wxLogin();
  },

  // 登录
  wxLogin : function() {
    let that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("-------" + res.code);
        if (res.code) {
          wx.request({
            url: cfg.host + cfg.djurl.wxLogin,
            data: { code: res.code },
            success: function (resObj) {
              console.log('服务器返回数据:' + resObj.data);
              if (1000 !== resObj.data.code) {
                console.log(resObj.data.message);
                return;
              }

              that.globalData.openId = resObj.data.data;
              console.log('>>>>>>>>openId=' + that.globalData.openId);
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            method: 'POST'
          })
        } else {
          console.log('获取用户信息失败');
        }
      }
    })
  },

  globalData: {
    openId: ''
  }
})